const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});
const db = cloud.database();

const logIn = async () => {
  const wxContext = cloud.getWXContext();
  const res = {};
  res.openID = wxContext.OPENID;

  const result = await db.collection('User_Profile').where({ OpenID: res.openID }).get();

  if (result.data.length > 0) {
    const user = result.data[0];
    res.isUnregistered=false;
    res.name = user.Name;
    res.nickname = user.Nickname;
    res.avatar = user.Avatar;
    res.gender = user.Gender;
    res.studentID = user.StudentID;
    res.studentType = user.StudentType;
    res.enrollmentYear = user.EnrollmentYear;
    res.college = user.College;
    res.major = user.Major;
    res.phoneNumber = user.PhoneNumber;
  }else{
    res.isUnregistered=true;
  }

  return res;
};

exports.main = async (event, context) => {
  return await logIn();
};
